package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmeobaCustomerListBean
import com.hinnotek.amoeba.bean.old.ProjectByCustomerBean
import com.hinnotek.amoeba.databinding.ActivityRenewalPaymentAddBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.ext.initAdapter
import com.hinnotek.amoeba.viewmodels.AmibaViewModel
import com.hinnotek.amoeba.viewmodels.PublicInterfaceViewModel
import com.lee.common.network.getRequestBody
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RenewalPaymentAddActivity : BaseActivity<ActivityRenewalPaymentAddBinding>() {

    private var orgId = 0
    private var customerId = 0
    private var amibaCustomerProjectId = 0
    private var approvalUserId = 0
    private var orgName: String = ""

    val viewModel by viewModels<PublicInterfaceViewModel>()
    val amibaViewModel by viewModels<AmibaViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_renewal_payment_add)

        setTitle("临时缴费/续费申请")
        setLeftVisible(true)
    }

    override fun initVew() {

        bindingView.superButtonSubmit.setOnClickListener(View.OnClickListener { v: View? ->
            if (ClickUtil.isFastClick()) {
                return@OnClickListener
            }
            val escRpChooseUnitS: String =
                bindingView.escRpChooseUnit.editText.getText().toString().trim()
            if (StringUtils.isEmpty(escRpChooseUnitS)) {
                ToastUtils.show(mContext, "请选择签署单位")
                return@OnClickListener
            }
            val projectName: String =
                bindingView.escRpChooseProject.editText.getText().toString().trim()
            if (StringUtils.isEmpty(projectName)) {
                ToastUtils.show(mContext, "请选择选择项目")
                return@OnClickListener
            }
            val usePosition: String = bindingView.etRpLocation.text.toString().trim()
            if (StringUtils.isEmpty(usePosition)) {
                ToastUtils.show(mContext, "请填写使用位置")
                return@OnClickListener
            }
            val etRpNumS: String = bindingView.etRpNum.text.toString().trim()
            if (StringUtils.isEmpty(etRpNumS)) {
                ToastUtils.show(mContext, "请填写缴费张数")
                return@OnClickListener
            }
            val flowQuota: String = bindingView.etRpFlow.text.toString().trim()
            if (StringUtils.isEmpty(flowQuota)) {
                ToastUtils.show(mContext, "请填写流量限额")
                return@OnClickListener
            }
            val payTotal: String = bindingView.etRpPayTotal.text.toString().trim()
            if (StringUtils.isEmpty(payTotal)) {
                ToastUtils.show(mContext, "请填写缴费总额")
                return@OnClickListener
            }
            val approvalUser: String =
                bindingView.escRpRelatedApprover.editText.getText().toString().trim()
            if (StringUtils.isEmpty(approvalUser)) {
                ToastUtils.show(mContext, "请选择关联审批人")
                return@OnClickListener
            }
            addAmibaTemporary(
                orgId,
                orgName,
                customerId,
                escRpChooseUnitS,
                amibaCustomerProjectId,
                projectName,
                usePosition,
                etRpNumS,
                payTotal,
                flowQuota,
                approvalUserId,
                approvalUser,
                2,
                "宁创"
            )
        })
        setData()
    }

    override fun setData() {
        orgId = SharedPreUtils.getInteger("orgId", 0)
        orgName = SharedPreUtils.getString("orgName")

        viewModel.getAmibaCustomerListByOrgId(orgId)
            .observe(this, object : ObserverState<MutableList<AmeobaCustomerListBean>>() {

                override fun onDataChange(data: MutableList<AmeobaCustomerListBean>) {
                    val stringArrays = arrayOfNulls<String>(data.size).apply {
                        for (index in data.indices) {
                            this[index] = data[index].customerName
                        }
                    }
                    bindingView.escRpChooseUnit.initAdapter(stringArrays)
                    bindingView.escRpChooseUnit.setOnItemClickListener { _, _, position, _ ->
                        customerId = data[position].customerId
                    }

                    viewModel.getProjectByCustomerId(customerId)
                        .observe(this@RenewalPaymentAddActivity,
                            object : ObserverState<MutableList<ProjectByCustomerBean>>() {
                                override fun onDataChange(data: MutableList<ProjectByCustomerBean>) {
                                    val arrayOfStrings = arrayOfNulls<String>(data.size)
                                    for (index in data.indices) {
                                        arrayOfStrings[index] = data[index].projectName
                                    }
                                    bindingView.escRpChooseUnit.initAdapter(arrayOfStrings)
                                    bindingView.escRpChooseUnit.setOnItemClickListener { _, _, position, id ->
                                        amibaCustomerProjectId =
                                            data[position].amibaCustomerProjectId
                                    }
                                    super.onDataChange(data)
                                }
                            })
                    super.onDataChange(data)
                }
            })

        viewModel.getProjectByCustomerId(customerId)
            .observe(this,
                object : ObserverState<MutableList<ProjectByCustomerBean>>() {
                    override fun onDataChange(data: MutableList<ProjectByCustomerBean>) {
                        val strings21 = arrayOfNulls<String>(data.size)
                        for (index in data.indices) {
                            strings21[index] = data[index].projectName
                        }
                        bindingView.escRpRelatedApprover.initAdapter(strings21)
                        bindingView.escRpRelatedApprover.setOnItemClickListener { _, _, position, id ->
                            approvalUserId = data[position].amibaCustomerProjectId
                        }
                        super.onDataChange(data)
                    }
                })
    }

    /**
     * app端添加临时缴费续费申请接口
     */
    fun addAmibaTemporary(
        orgId: Int, orgName: String, customerId: Int, customerName: String,
        amibaCustomerProjectId: Int, projectName: String,
        usePosition: String, payNum: String, payTotal: String,
        flowQuota: String, approvalUserId: Int,
        approvalUser: String, sendUserId: Int, sendUser: String
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        val map = mutableMapOf<String,Any>().apply {
            put("applyUserId", SharedPreUtils.getInteger("userId",0))
            put("orgId", orgId)
            put("orgName", orgName)
            put("customerId", customerId)
            put("customerName", customerName)
            put("amibaCustomerProjectId", amibaCustomerProjectId)
            put("projectName", projectName)
            put("usePosition", usePosition)
            put("payNum", payNum)
            put("payTotal", payTotal)
            put("flowQuota", flowQuota)
            put("approvalUserId", approvalUserId)
            put("approvalUser", approvalUser)
            put("sendUserId", sendUserId)
            put("sendUser", sendUser)
            put("createUser", SharedPreUtils.getString("personName"))
        }
        amibaViewModel.addAmibaTemporary(getRequestBody(map)).observe(this,object : ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                finishActivity()
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }
}